from .utils.yuntongxun.sms import CCP
import logging
from celery_tasks.main import celery_app

logger = logging.getLogger('django')


# 参数都由任务队列传进来
@celery_app.task(name='send_sms_code')
def send_sms_code(mobile, sms_code, expires, temp_id):
    '''发送短信验证码'''
    # 只是换个人做,就直接复制
    # 只用干活,不用返回
    try:
        ccp = CCP()
        result = ccp.send_template_sms(mobile, (sms_code, expires), temp_id)

    except Exception as e:
        logger.error('发送短信验证码异常[mobile:%s, message:%s]' % (mobile, e))
        # return Response({'message': 'failed'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    else:
        if result == 0:
            logger.info('发送短信验证码正常[mobile:%s]' % mobile)
            # return Response({'message': 'ok'})
        else:
            logger.warning('发送短信验证码失败[mobile%s]' % mobile)
            # return Response({'message': 'failed'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
            # 假装成功
            # return Response({'message': 'ok'})